<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all"/>
    <style>
        .layui-form-item .layui-input-inline {
            width: 150px;
        }
        .layui-form-label {
            padding: 9px 5px;
            width: auto;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <form class="layui-form" id="sbumitForm">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline ">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input"/>
                        </div>
                    </div>
                    <div class="layui-inline ">
                        <label class="layui-form-label">显示端</label>
                        <div class="layui-input-inline" style="width: 80px">
                            <select name="equipment" lay-filter="equipment">
                                <option value="">全部</option>
                                <option value="1">PC端</option>
                                <option value="2">APP端</option>
                            </select>
                        </div>
                    </div>
                    <!---
                    <div class="layui-inline ">
                        <label class="layui-form-label">时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="start_time" class="layui-input" id="start_date" readonly placeholder="开始日期">
                        </div>
                        <div class="layui-input-inline" id="maindiv">
                            <input type="text" name="end_time" class="layui-input" id="end_date" readonly placeholder="结束日期">
                        </div>
                    </div>-->
                    <div class="layui-inline ">
                        <label class="layui-form-label">广告位置</label>
                        <div class="layui-input-inline" style="width: 120px">
                            <select name="type" id="typeSelect">
                                <option value="">全部</option>
                                {foreach $type_cate as $key=>$value}
                                <option value="{$key}">{$value}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline ">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline" style="width: 80px">
                            <select name="status">
                                <option value="">全部</option>
                                <option value="0">失效</option>
                                <option value="1">生效</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline ">
                        <label class="layui-form-label">操作人</label>
                        <div class="layui-input-inline">
                            <input type="text" name="operator" placeholder="请输入操作人账号" autocomplete="off" class="layui-input"/>
                        </div>
                    </div>
                    <div class="layui-inline" style="width:80px;">
                        <div class="layui-input-inline" style="width:80px;">
                            <div class="layui-btn" id="submit">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline" style="width:80px;">
                        <div class="layui-input-inline" style="width:80px;">
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline ">
                        <div class="layui-input-inline">
                            <div class="layui-btn" onclick="add()">
                                <i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>添加
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>

        <div class="layui-card-body layui-table-body layui-table-main">
            <table id="demo" lay-filter="demo"></table>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script>
    var types = <?php echo $type_json;?>;
    layui.config({
        base: "/layuiadmin/" //静态资源所在路径
        , version: "{:config('other.ui_version')}"
    }).extend({
        index: "lib/index", //主入口模块
    }).use(["table", "laydate", 'form'], function () {
        $ = layui.jquery, form = layui.form, table = layui.table, laydate = layui.laydate;
        laydate.render({
            elem: '#start_date', //指定元素
            max: '<?php echo date("Y-m-d");?>',
            done: function (value, date, endDate) {
                $("#maindiv").empty();
                $("#maindiv").append('<input type="text" type="text" name="end_time" class="layui-input"  id="end_date" readonly  placeholder="结束日期" />');
                laydate.render({
                    elem: '#end_date', //指定元素
                    min: value,
                    max: '<?php echo date("Y-m-d");?>',
                });
            },
        });
        laydate.render({
            elem: '#end_date', //指定元素
            max: '<?php echo date("Y-m-d");?>',
        });
        form.on('select(equipment)', function (data) {
            if (data.value == 1) {
                $("#typeSelect").html(`
                        <option value="">全部</option>
                        <option value="6">首页背景图</option>
                        <option value="10"> 直播页轮播图</option>
                        <option value="11">顶部导航背景图</option>
                        <option value="12">商城首页轮播图</option>
                        `
                );
            }

            if (data.value == 2) {
                $("#typeSelect").html(`
                        <option value="">全部</option>
                        <option value="1">启动页广告</option>
                        <option value="2">首页轮播广告</option>
                        <option value="12">商城首页轮播图</option>
                        `
                );
            }

            if (data.value == "") {
                var selectHtml = '<option value="">全部</option>';
                for (let val in types) {
                    selectHtml += '<option value="' + val + '">' + types[val] + '</option>';
                }

                $("#typeSelect").html(selectHtml);
            }

            form.render();
        });
        $("#submit").click(function () {
            table.render(table_conf());
        });

        table.render(table_conf());
    });

    function table_conf() {
        return {
            elem: '#demo',
            id: '#demo',
            url: '/admin/ads/index?' + $("#sbumitForm").serialize(),
            defaultToolbar: [
                'filter', 'print', {title: '提示', layEvent: 'LAYTABLE_EXCEL', icon: 'layui-icon-table'},
            ],
            page: {
                curr: 1   //默认从第一页开始搜索
            },
            method: "get",
            //where: $("#sbumitForm").serialize(),
            limit: 20,
            limits: [20, 30, 50, 100, 200, 500],
            text: {
                none: '暂无相关数据！'//默认无数据
            },
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 0,//成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'list', //数据列表的字段名称，默认：data
            },
            cols: [[
                {field: 'id', width: 70, title: 'ID', sort: true, align: 'center'},
                {field: 'title', width: 200, title: '标题', align: 'center'},
                {
                    field: 'equipment', width: 110, title: '显示端', align: 'center', templet: function (d) {
                        switch (d.equipment) {
                            case 1:
                                return 'PC';
                            case 2:
                                return 'APP';
                        }

                        return '未知';
                    }
                },
                {
                    field: 'type', width: 160, title: '广告位置', align: 'center', templet: function (d) {
                        if (types[d.type]) {
                            return types[d.type];
                        }

                        return '未知';
                    }
                },
                { field: 'sort', width: 70, title: '排序', align: 'center' },
                {field: 'start_time', width: 180, title: '开始时间', align: 'center'},
                {field: 'end_time', width: 180, title: '结束时间', align: 'center'},
                {
                    field: '', width: 100, title: '状态', align: 'center', templet: function (d) {
                        if(d.status == 1) {
                            return '<span class="layui-badge layui-bg-green">生效中</span>';
                        }else if (d.status == 2){
                            return '<span class="layui-badge layui-bg-orange">已过期</span>';
                        }else{
                            return '<span class="layui-badge layui-bg-red">禁用</span>';
                        }
                    }
                },
                {field: 'operator', width: 120, title: '操作人', align: 'center'},
                {
                    title: '操作', align: 'center', templet: function (item) {
                        var html = '<a class="layui-btn layui-btn-xs" onclick="edit(' + item['id'] + ')"><i class="layui-icon"></i>编辑</a><a class="layui-btn layui-btn-danger layui-btn-xs" onclick="del(' + item['id'] + ')"><i class="layui-icon"></i>删除</a>';

                        if (item.status) {
                            html += '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="enable(' + item['id'] + ', 0)"><i class="layui-icon"></i>禁用</a>';
                        } else {
                            html += '<a class="layui-btn layui-btn-normal layui-btn-xs"  onclick="enable(' + item['id'] + ', 1)"><i class="layui-icon"></i>启用</a>';
                        }

                        return html;
                    }
                }
            ]],
            done: function (res, curr, count) {
                res.list.forEach(function (item, index) {

                });
            },
        };
    }

    function enable(id, status) {
        var title = status == 1 ? '启用' : '禁用';
        layer.confirm('确认要' + title + '吗？', function (index) {
            $.ajax({
                type: "POST",//请求方式
                url: '/admin/ads/enable', //请求地址
                dataType: "json",//返回的数据类型
                data: {"id": id, "status": status},
                //请求成功
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                }
            });
        });
    }

    /*添加*/
    function add() {
        layer.open({
            type: 2,
            title: ["添加", 'font-size:16px'],
            content: '/admin/ads/add',
            area: ['680px', '600px'],
            //offset: '0px',
        });
    }

    function edit(id) {
        layer.open({
            type: 2,
            title: ["编辑", 'font-size:16px'],
            content: '/admin/ads/edit?id=' + id,
            area: ['680px', '600px'],
            //offset: '0px',
        });
    }

    function del(id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: "POST",//请求方式
                url: '/admin/ads/del', //请求地址
                dataType: "json",//返回的数据类型
                data: {"id": id},
                //请求成功
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                }
            });
        });
    }
</script>
</body>
</html>
